package com.dnjn.happiness.biz.controller.common;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.biz.domain.Checkup;
import com.dnjn.happiness.biz.domain.vo.CheckupVO;
import com.dnjn.happiness.biz.service.ICheckupService;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.core.utils.bean.BeanUtils;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;

/**
 * 体检Controller
 *
 * @author happiness
 * @date 2024-03-27
 */

public class CommonCheckupController extends BaseController {
    @Autowired
    protected ICheckupService checkupService;

    /**
     * 查询体检列表
     */
    @Log(title = "体检" , businessType = BusinessType.LIST)
    @RequiresPermissions("@ss.hasAnyPermi('biz:checkup:list,ignore:checkup:list')" )
    @PostMapping("/list" )
    public AjaxResult list(@RequestBody CheckupVO vo, @RequestParam(defaultValue = "1" ) Integer current, @RequestParam(defaultValue = "10" ) Integer size) {
        Checkup checkup = new Checkup();
        BeanUtils.copyPropsIgnoreNull(vo, checkup);
        Page page = Page.of(current, size);
        Page<Checkup> list = checkupService.getPage(page, checkup);
        return success(list);
    }

    /**
     * 获取体检详细信息
     */
    @RequiresPermissions("@ss.hasAnyPermi('biz:checkup:query,ignore:checkup:query')" )
    @GetMapping(value = "/{checkupId}" )
    @Log(title = "biz" , businessType = BusinessType.SELECT)
    public AjaxResult get(@PathVariable("checkupId" ) Long checkupId) {
        return success(checkupService.getById(checkupId));
    }

    /**
     * 新增体检
     */
    @Log(title = "体检" , businessType = BusinessType.INSERT)
    @RequiresPermissions("@ss.hasAnyPermi('biz:checkup:add,ignore:checkup:add')" )
    @PostMapping("/add" )
    public AjaxResult add(@RequestBody CheckupVO vo) {
        Checkup checkup = new Checkup();
        BeanUtils.copyPropsIgnoreNull(vo, checkup);

        checkup.setCreateBy(SecurityUtils.getUsername());
        return toAjax(checkupService.save(checkup));
    }

    /**
     * 修改体检
     */
    @Log(title = "体检" , businessType = BusinessType.UPDATE)
    @RequiresPermissions("@ss.hasAnyPermi('biz:checkup:edit,ignore:checkup:edit')" )
    @PutMapping("/edit" )
    public AjaxResult edit(@RequestBody CheckupVO vo) {
        Checkup checkup = new Checkup();
        BeanUtils.copyPropsIgnoreNull(vo, checkup);
        return toAjax(checkupService.updateById(checkup));
    }

    /**
     * 删除体检
     */
    @Log(title = "体检" , businessType = BusinessType.DELETE)
    @RequiresPermissions("@ss.hasAnyPermi('biz:checkup:remove,ignore:checkup:remove')" )
    @DeleteMapping("/{checkupIds}" )
    public AjaxResult remove(@PathVariable Long[] checkupIds) {
        return toAjax(checkupService.removeBatchByIds(Arrays.asList(checkupIds)));
    }
}
